//
//  XCSongListButtonViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/11.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCSongListButtonViewController.h"
#import "XCSingListButtonTableViewCell.h"
#import "XCAFNetTool.h"
#import "XCUrlManger.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "XCInTheSongListViewController.h"

@interface XCSongListButtonViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) UIImageView *songListImagView;

@property (nonatomic, retain) NSNumber *quan_id;
@end

@implementation XCSongListButtonViewController

- (void)dealloc {
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    
    [_tableView release];
    [_dataSource release];
    [_songListImagView release];
    [_quan_id release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"歌单广场";
    self.dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    self.page = 1;
    
    [self songListTableViewBuild];
    
    [self.tableView addFooterWithTarget:self action:@selector(songListNetRequest)];
    [self.tableView footerBeginRefreshing];
    self.tableView.footerPullToRefreshText = @"上拉刷新数据";
    self.tableView.footerRefreshingText = @"正在刷新数据";
    self.tableView.footerReleaseToRefreshText = @"松开刷新数据";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    
    
}
- (void)songListNetRequest {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger songListButtonMusic:self.page] body:nil headerFile:nil response:XCJSON success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            XCSingListButtonModel *singListButtonModel = [[XCSingListButtonModel alloc] initWithDataSource:dic];
            
            [self.dataSource addObject:singListButtonModel];
            [singListButtonModel release];
        }
        
        
        [self.tableView reloadData];
        [self.tableView footerEndRefreshing];
        self.page += 1;
    
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
    }];
}
- (void)songListTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStyleGrouped];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 4)];
    self.songListImagView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -100, wide, high / 4 + 70)];
    
    self.songListImagView.image = [UIImage imageNamed:@"Image-p"];
    [view addSubview:self.songListImagView];
    self.tableView.tableHeaderView = view;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];

    [self.tableView registerClass:[XCSingListButtonTableViewCell class] forCellReuseIdentifier:@"XCSingListButtonTableViewCell"];
    
    [self.view addSubview:self.tableView];
    [_tableView release];
}
#pragma mark - 方法实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCSingListButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCSingListButtonTableViewCell"];
    XCSingListButtonModel *singListButtonModel = self.dataSource[indexPath.row];
    cell.singListButtonModel = singListButtonModel;
    cell.layer.transform = CATransform3DMakeRotation (M_PI/2, 0, 0, 1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1-(scrollView.contentOffset.y/self.view.bounds.size.height / 2);
    scale = (scale>=1)?scale :1;
    self.songListImagView.transform = CGAffineTransformMakeScale(scale, scale);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XCSingListButtonModel *singListButtonModel = self.dataSource[indexPath.row];
    self.quan_id = singListButtonModel.quan_id;
    XCInTheSongListViewController *inTheSongListVC = [[XCInTheSongListViewController alloc] initWithQuan_id:self.quan_id withTitle:singListButtonModel.title];
    [self.navigationController pushViewController:inTheSongListVC animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
